$(document).ready(function(){

	getNotPay();
	function getNotPay(){
		var date=new Date();
		var orderDate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
		$.ajax({
			url:'dishOrder_searchDataListByDate.action',
			dataType:'json',
			type:'POST',
			data:{
				'orderDate':orderDate,
			},
			success:function(data){
				if (data.status == "200"){
					for(var i=0,j=1;i<data.data.length;i++,j++){
						var value=data.data[i];
					$('#order-list1').after(" <li class='list-group-item notPay' >"+
						"<table style='width: 100%;' class='table'>"+
							"<tr class='first-title' id='"+value.serialsNumber+"' data-flag='true'>"+
								"<td>"+j+"</td>"+
								"<td>"+value.diningTableName+"</td>"+
								"<td>流水号:"+value.serialsNumber+"</td>"+
								"<td>开台时间:"+value.creatTime+"</td>"+
								"<td>￥"+value.price+"</td>"+
								"<td><button class='btn btn-warning payfor'>结账</button></td><td><span class='glyphicon glyphicon-th-list expand' style='cursor:pointer;'></span></td></tr></table></li>");
					}
				} else {
//				window.location.href = "error.html";
				}
			}
		}).done(function(){
			$('.expand').unbind('click').on('click',function(){	
				var Number=$(this).parents('.first-title').children().eq(2).text();
//				alert($(this).parents('.notPay').find('.first-title').attr('class'));
				var serialsNumber=Number.substr(4,Number.length-4);
				var class_name=$(this).attr("class");
				if($('#'+serialsNumber).attr("data-flag")){
					$.ajax({
						url:'dishOrderItem_searchDataListBySerialsNumber.action',
						dataType:'json',
						type:'POST',
						data:{
							'serialsNumber':serialsNumber
						},
						success:function(data){
							$('#'+serialsNumber).nextAll().remove();
							for(var i=0;i<data.data.length;i++){
								var value=data.data[i];
								if(value.enumCode==1||value.enumCode==2){
									$('#'+serialsNumber).after("<tr class='chashier-order-detail' >"+
											"<td></td>"+
											"<td>"+value.dishName+"</td>"+
											"<td>X"+value.dishNum+"</td>"+
											"<td>￥"+value.price+"</td>"+
											"<td>"+value.enumName+"</td><td><a href='#' class='btn btn-warning server-food' data-id='"+value.id+"' disabled>上菜</a></td><td><a href='#' class='btn btn-danger quit-food' data-toggle='modal' data-target='#myModal' data-id='"+value.id+"' disabled>退菜</a></td></tr>");
								}
								else{
									$('#'+serialsNumber).after("<tr class='chashier-order-detail' >"+
											"<td></td>"+
											"<td>"+value.dishName+"</td>"+
											"<td>X"+value.dishNum+"</td>"+
											"<td>￥"+value.price+"</td>"+
											"<td>"+value.enumName+"</td><td><a href='#' class='btn btn-warning server-food' data-id='"+value.id+"'>上菜</a></td><td><a href='#' class='btn btn-danger quit-food' data-toggle='modal' data-target='#myModal' data-id='"+value.id+"'>退菜</a></td></tr>");
								}
								//alert(value.id);
							}
							$('#'+serialsNumber).nextAll().show();
							$('#'+serialsNumber).attr({'data-flag':null});
						}
					}).done(function(){
						//expand();
					});
				}else{
					$('#'+serialsNumber).nextAll().hide();
					$('#'+serialsNumber).attr({'data-flag':true});
				}
				
			});

		});
	};
//	结账
	$('body').on('click','.payfor',function(){
		var Number=$(this).parents('.first-title').children().eq(2).text();
		var serialsNumber=Number.substr(4,Number.length-4);
		$.ajax({
			url:'dishOrder_payForOrder.action',
			dataType:'json',
			type:'POST',
			data:{
				'serialsNumber':serialsNumber
			},
			success:function(data){
				if (data.status == "200"){
					$('.notPay').remove();
					getNotPay();
					$.session.remove('diningTableName');
					$.session.remove('diningTableID');
				}
				else{
//					window.location.href = "error.html";
				}
			}
		});
	});
//	退菜
	food_back();
	function food_back(){
		var id;
		var yy1;
		var yy2;
		var yy3;
		$('body').on('click','.quit-food',function(){
			id=$(this).attr('data-id');
			yy1=$(this).parent().prev().prev();
			yy2=$(this);
			yy3=$(this).parent().prev().children();
		});
		$('body').on('click','.quit-food-btn',function(){
			var reason=$('.food-back-reason').val();
			$.ajax({
				url:'dishOrderItem_changeDataState.action',
				dataType:'json',
				type:'POST',
				data:{
					'reason':$.trim(reason),
					'id':id,
					'enumName':'已退菜',
					'enumCode':2
				},
				success:function(){
					yy1.text('已退菜');
					yy2.attr("disabled",true);
					yy3.attr("disabled",true);
				}
			});
	});
	}
//	上菜
	server_food();
	function server_food(){
		$('body').on('click','.server-food',function(){
			var yy1=$(this).parent().prev();
			var yy2=$(this);
			var yy3=$(this).parent().next().children();
			var id=$(this).attr('data-id');
			$.ajax({
				url:'dishOrderItem_changeDataState.action',
				dataType:'json',
				type:'POST',
				data:{
					'id':id,
					'enumName':'已上菜',
					'enumCode':1
				},
				success:function(){
					yy1.text('已上菜');
					yy2.attr("disabled",true);
					yy3.attr("disabled",true);
				}
			});
		});
	}

	
	
});
